package cn.fl.custpay.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cmm.mid.core.framework.page.KeyValueVO;
import cmm.user.facade.framework.constant.DictionaryConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.utils.*;
import cn.fl.custpay.api.ICustPayCfgMApiService;
import cn.fl.custpay.dto.CustPayCfgMDTO;
import cn.fl.custpay.service.ICustPayCfgMService;
import cn.fl.custpay.transform.CustPayCfgMVOTransform;
import cn.fl.custpay.vo.CustPayCfgMVO;
import cn.fl.system.service.ISysDictionaryService;
import fl.constant.facade.framework.constant.CmmCustPayConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.fl.framework.base.context.CurrentThreadContext;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.exception.BaseException;

/**
 * @Description: 支付渠道配置主表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class CustPayCfgMServiceImpl implements ICustPayCfgMService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(ICustPayCfgMService.class);
	
	/**
	 * 渠道暴漏API
	 */
	@Autowired
	private ICustPayCfgMApiService custPayCfgMApiService;

	@Autowired
	private ISysDictionaryService sysDictionaryService;

	@Override
	public CustPayCfgMVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		CustPayCfgMDTO dto = this.custPayCfgMApiService.selectById(id);
		return CustPayCfgMVOTransform.toVO(dto);
	}

	@Override
	public Long add(CustPayCfgMVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		return this.custPayCfgMApiService.add(CustPayCfgMVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		return this.custPayCfgMApiService.removeById(id);
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
    		BaseLogUtils.info(logger, "removeByIds", "删除主键参数集合不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		return false;
    }

	@Override
	public boolean modify(CustPayCfgMVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		return this.custPayCfgMApiService.modify(CustPayCfgMVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
        Pager pager = this.custPayCfgMApiService.searchListPage(page);
        List<CustPayCfgMDTO> resultList = (List<CustPayCfgMDTO>) pager.getResultList();
        //数据字典（是否启用）
        List<String> codeGrpCdList = new ArrayList<>();
        codeGrpCdList.add(DictionaryConstant.CODE_GRP_IS_VALID_CD);
        codeGrpCdList.add(CmmCustPayConstant.CODE_PAY_TYPE_CD);
        Map<String, List<KeyValueVO>> dicMap = this.sysDictionaryService.getMultiKvList(CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd(), codeGrpCdList);
        List<KeyValueVO> statusList = dicMap.get(DictionaryConstant.CODE_GRP_IS_VALID_CD);
        Map<String, String> statusMap = null;
        if (CollectionUtils.isNotEmpty(statusList)) {
            statusMap = new HashMap<>(statusList.size());
            for (KeyValueVO dto : statusList) {
                statusMap.put(dto.getKey(), dto.getValue());
            }
        }
        List<KeyValueVO> payTypeList = dicMap.get(CmmCustPayConstant.CODE_PAY_TYPE_CD);
        Map<String, String> payTypeMap = null;
        if (CollectionUtils.isNotEmpty(payTypeList)) {
            payTypeMap = new HashMap<>(payTypeList.size());
            for (KeyValueVO dto : payTypeList) {
                payTypeMap.put(dto.getKey(), dto.getValue());
            }
        }
        List<CustPayCfgMVO> custPayCfgMVOList = CustPayCfgMVOTransform.toVOList(resultList);
        for (CustPayCfgMVO custPayCfgMVO : custPayCfgMVOList) {
            if (statusMap != null && statusMap.containsKey(String.valueOf(custPayCfgMVO.getIsValid()))) {
                custPayCfgMVO.setIsValidNm(statusMap.get(String.valueOf(custPayCfgMVO.getIsValid())));
                custPayCfgMVO.setPayTypeNm(payTypeMap.get(String.valueOf(custPayCfgMVO.getPayType())));
                if (StringUtils.isNotEmpty(custPayCfgMVO.getFeeAmt())) {
					custPayCfgMVO.setFeeAmtNm(DecimalUtils.convertFenToYuan(custPayCfgMVO.getFeeAmt()));
				}
            }
        }
        pager.setResultList(custPayCfgMVOList);
		statusMap.clear();
		payTypeMap.clear();
		statusMap = null;
		payTypeMap = null;
		return pager;
	}

	@Override
	public Boolean modifyRMIsvalid(Long id, int isValid) {
		if(null == id){
			BaseLogUtils.info(logger, "modifyRMIsvalid", "修改支付渠道管理主表状态失败,支付渠道管理管理主表对象参数为空.DATA:"+ id, CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		return this.custPayCfgMApiService.modifyRMIsValid(id, isValid);
	}
}
